#pragma once
#include "protocol.hpp"
#include <iostream>

enum
{
    Div_Zero = 1,
    Mod_Zero,
    No_op,
};

class Calculator
{
public:
    Calculator()
    {}

    Respone CalculatorHandler(const Request& rqt)
    {
        Respone rsp(0, 0);
        switch (rqt._op)
        {
        case '+':
            rsp._result = rqt._x + rqt._y;
            break;
        case '-':
            rsp._result = rqt._x - rqt._y;
            break;
        case '*':
            rsp._result = rqt._x * rqt._y;
            break;
        case '/':
        {
            if(rqt._y == 0)
            {
                rsp._code = Div_Zero;
                break;
            }
            rsp._result = rqt._x / rqt._y;
            break;    
        }
        case '%':
       {
            if(rqt._y == 0)
            {
                rsp._code = Mod_Zero;
                break;
            }
            rsp._result = rqt._x % rqt._y;
            break;    
        }
        default:
            rsp._code = No_op;
            break;
        }

        return rsp;
    }

    // 传递一个请求报文 要求解决问题
    std::string Handler(std::string& package)
    {
        // 去掉报头， 得到有效载荷
        std::string content;
        if(Decode(package, content) == false)
            return "";
        std::cout << "Decode" << std::endl;
        // 反序列化 得到内容
        Request req;
        if(req.deserialize(content) == false)
            return "";

        // 根据内容得答案
        Respone resp = CalculatorHandler(req);

        // 将答案序列化
        content = "";
        resp.serialize(content);

        // 添加报头
        content = Encode(content);

        return content;
    }

};
